import {
  chatMessageList,
  chatMessageAsk,
  conversationUpdate,
  conversationNew,
  conversationList,
  conversationDelete,
  userSettingInfoUpdate,
  userSettingInfo,
  chatModelList,
} from '@/network/api'
import globalConfig, { globalHint, globalStatus } from '@/common/config'

const chatAI = {
  state: {
    newChatId: 1,
    chatListAI: [],
    saveconversationList: [],
    nowItem: {
      name: '',
    },
    share: false,
    systemRole: {},
    systemRole2: {},
    keyboardHeight: 0,
    modelList: [],
    settingUserInfo: {},
    hidefoucs: false,
  },
  getters: {},
  mutations: {
    SETFOUCE(state, params) {
      state.hidefoucs = params
    },
    SETHEIGHT(state, params) {
      state.keyboardHeight = params
    },
    SETMODELLIST(state, params) {
      state.modelList = params
    },
    SETSYSTEMROLE(state, params) {
      state.systemRole = params
      state.systemRole2 = params
    },
    SETNEWID(state, params) {
      state.newChatId = params
    },
    SETSHARE(state, params) {
      state.share = params
    },

    SAVECHATLIST(state, params) {
      console.log('新建进来', params)
      if (Array.isArray(params)) {
        state.chatListAI = [...state.chatListAI, ...params]
      } else {
        let tempflag = false
        if (state.chatListAI.length > 1) {
          state.chatListAI.forEach((item) => {
            if (item.tempIndex) {
              if (item.tempIndex == params.tempIndex) {
                item.content = params.content || params.answer_content
                item.role = params.role
                if (params.status == 'sending') {
                  setTimeout(() => {
                    item.status = 'done'
                    console.log('看看item', item)
                  }, 2000)
                }

                item.status = params.status
                item.avatar = params.avatar || params.model.avatar_url
                tempflag = true
              }
            }
          })
        }

        if (tempflag) return
        let tempImg = ''
        if (params.model) {
          tempImg = params.model.avatar_url
        }
        if (params.user) {
          tempImg = params.user.avatar_url
        }
        state.chatListAI.push({
          content: params.content ? params.content : params.answer_content,
          role: params.role,
          status: params.status,
          avatar: params.avatar || tempImg,
          tempIndex: params.tempIndex,
        })
      }
    },
    SAVELIST(state, params) {
      state.saveconversationList = params
      // state.saveconversationList = Array.from(new Set([...state.saveconversationList, ...params]))
    },
    SAVEITEM(state, params) {
      state.nowItem = params
    },
    SETNAME(state, params) {
      state.nowItem.name = params
    },
    CLEARLIST(state) {
      state.chatListAI = []
    },
    SETTINGUSERINFO(state, params) {
      state.settingUserInfo = params
    },
    myAddHistoryItem(state, data) {
      state.nowItem = {
        id: state.newChatId,
        status: globalConfig.hint,
        role: globalConfig.robotRole,
        content: data.desc,
        time: new Date().getTime(),
        name: data.title,
      }
      // // 这里id应该要接口返回的
      // state.chatListAI.push()
    },
  },
  actions: {
    // 聊天内容列表
    async IchatMessageList({ commit }, params) {
      try {
        const res = await chatMessageList(params)
        console.log(res)
        commit('SAVECHATLIST', res.list)
      } catch (error) {
        console.log(error)
      }
    },
    // 会话删除
    async IconversationDelete(state, params) {
      try {
        const res = await conversationDelete(params)
        console.log(res)
        return res
      } catch (error) {
        return Promise.reject(error)
        console.log(error)
      }
    },
    // 新建会话
    async IconversationNew({ commit }, params) {
      try {
        const res = await conversationNew(params)
        commit('SETNEWID', res.id)
        return res
      } catch (error) {
        console.log(error)
      }
    },
    // 会话修改
    async IconversationUpdate(state, params) {
      try {
        const res = await conversationUpdate(params)
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 聊天提问
    async IchatMessageAsk({ commit, state }, params) {
      let tempIndex = params.tepId
      let tempava = state.chatListAI[0].model || state.chatListAI[0]
      try {
        console.log('tempava', tempava)
        console.log('tempavaavatar_url', tempava.avatar_url)
        console.log('tempavaavidl', tempava.id)
        commit('SAVECHATLIST', {
          answer_content: '',
          model: {
            avatar_url: tempava.avatar_url || tempava.avatar,
            id: tempava.id,
          },
          role: 'assistant',
          status: 'loading',
          tempIndex,
        })
        console.log('tempava---', tempava)
        const res = await chatMessageAsk(params)
        console.log(res)
        const { answer_content, model, role } = res
        commit('SAVECHATLIST', {
          answer_content,
          model,
          role,
          status: 'sending',
          tempIndex,
        })
        // setTimeout(() => {
        //   commit('SAVECHATLIST', {
        //     answer_content,
        //     model,
        //     role,
        //     status: 'done',
        //     tempIndex,
        //   })
        // }, 2000)
      } catch (error) {
        commit('SAVECHATLIST', {
          answer_content: '',
          model: {
            avatar_url: tempava.avatar_url,
            id: tempava.id,
          },
          role: 'assistant',
          status: 'error',
          tempIndex,
        })
        uni.showToast({
          title: error.message,
          icon: 'none',
          duration: 2000,
        })
        console.log(error)
      }
    },
    // 会话列表

    async IconversationList({ commit }, params) {
      try {
        const res = await conversationList(params)
        console.log(res)
        commit('SAVELIST', res.list)
        return res.list
      } catch (error) {
        console.log(error)
      }
    },
    // 用户设置
    async IuserSettingInfoUpdate({ commit }, params) {
      try {
        const res = await userSettingInfoUpdate(params)
        console.log(res)
        uni.showToast({
          title: '更新成功',
          icon: 'none',
          duration: 1000,
        })
        return res
      } catch (error) {
        return Promise.reject(error)
        console.log(error)
      }
    },
    // 模特列表
    async IchatModelList({ commit }, params) {
      try {
        const res = await chatModelList({ page: 1, limit: 100 })
        console.log('存模特列表', res.list)
        commit('SETMODELLIST', res.list)
        return res.list
      } catch (error) {}
    },
    // 用户设置 获取
    async IuserSettingInfo({ commit }, params) {
      try {
        const res = await userSettingInfo(params)
        console.log(res)
        commit('SETTINGUSERINFO', res.info)
        return res.info
      } catch (error) {}
    },
  },
}
export default chatAI
